package com.zyf.lj.controller;

import com.zyf.lj.entity.JobInfo;
import com.zyf.lj.service.JobInfoService;
import com.zyf.lj.vo.PageResult;
import com.zyf.lj.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 招聘信息控制器
 */
@RestController
@RequestMapping("/api/employment")
public class JobInfoController {
    
    @Autowired
    private JobInfoService jobInfoService;
    
    /**
     * 分页查询招聘信息
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param keyword 搜索关键词
     * @param industry 行业
     * @param jobType 职位类型
     * @param orderBy 排序字段
     * @param orderType 排序类型
     * @return 分页结果
     */
    @GetMapping("/jobs")
    public Result<PageResult<JobInfo>> getJobInfoPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String industry,
            @RequestParam(required = false) String jobType,
            @RequestParam(required = false) String orderBy,
            @RequestParam(required = false) String orderType) {
        
        PageResult<JobInfo> pageResult = jobInfoService.getJobInfoPage(
                pageNum, pageSize, keyword, industry, jobType, orderBy, orderType);
        
        return Result.success(pageResult);
    }
    
    /**
     * 获取最新招聘信息
     * @param limit 限制数量
     * @return 招聘信息列表
     */
    @GetMapping("/jobs/latest")
    public Result<List<JobInfo>> getLatestJobInfos(
            @RequestParam(defaultValue = "5") Integer limit) {
        
        List<JobInfo> jobInfos = jobInfoService.getLatestJobInfos(limit);
        
        return Result.success(jobInfos);
    }
    
    /**
     * 根据ID获取招聘信息详情
     * @param id 招聘信息ID
     * @return 招聘信息详情
     */
    @GetMapping("/job/{id}")
    public Result<JobInfo> getJobInfoById(@PathVariable Integer id) {
        JobInfo jobInfo = jobInfoService.getJobInfoById(id);
        
        if (jobInfo != null) {
            return Result.success(jobInfo);
        } else {
            return Result.fail("招聘信息不存在");
        }
    }
    
    /**
     * 添加招聘信息
     * @param jobInfo 招聘信息对象
     * @return 添加结果
     */
    @PostMapping("/job")
    public Result<Void> addJobInfo(@RequestBody JobInfo jobInfo) {
        boolean success = jobInfoService.addJobInfo(jobInfo);
        
        if (success) {
            return Result.success();
        } else {
            return Result.fail("添加招聘信息失败");
        }
    }
    
    /**
     * 更新招聘信息
     * @param jobInfo 招聘信息对象
     * @return 更新结果
     */
    @PutMapping("/job")
    public Result<Void> updateJobInfo(@RequestBody JobInfo jobInfo) {
        boolean success = jobInfoService.updateJobInfo(jobInfo);
        
        if (success) {
            return Result.success();
        } else {
            return Result.fail("更新招聘信息失败");
        }
    }
    
    /**
     * 删除招聘信息
     * @param id 招聘信息ID
     * @return 删除结果
     */
    @DeleteMapping("/job/{id}")
    public Result<Void> deleteJobInfo(@PathVariable Integer id) {
        boolean success = jobInfoService.deleteJobInfoById(id);
        
        if (success) {
            return Result.success();
        } else {
            return Result.fail("删除招聘信息失败");
        }
    }
} 